package lc.com.androidproject.base;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import lc.com.androidproject.R;
import lc.com.architecture.base.BaseFragment;
import lc.com.architecture.entity.MessageInfo;
import lc.com.architecture.i.IPresenter;
import lc.com.architecture.utils.DeviceUtil;
import lc.com.architecture.utils.StatusBarUtils;

/**
 * @author LiuChao
 * @describe
 * @date 2017/11/14
 * @contact email:450127106@qq.com
 */

public abstract class LCBaseFragment<P extends IPresenter> extends BaseFragment<P> {

    private static final int DEFAULT_STATUSBAR_COLOR = R.color.colorAccent;
    protected View mStatusPlaceholderView;

    @Override
    public View getContentView() {
        LinearLayout rootView = new LinearLayout(getActivity());
        rootView.setOrientation(LinearLayout.VERTICAL);
        rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initStatusBarView(rootView);
        initBodyView(rootView);
        return rootView;
    }


    /**
     * 使用一个和状态栏等高的view
     *
     * @return
     */
    protected boolean useStatusBarSpaceView() {
        return true;
    }

    /**
     * 是否要求状态栏透明
     *
     * @return
     */
    protected boolean statusBarTranslate() {
        return false;
    }

    protected int getStatusBarColor() {
        return DEFAULT_STATUSBAR_COLOR;
    }

    protected abstract int getBodyLayoutId();

    /**
     * 初始化状态栏
     *
     * @param rootView
     */
    private void initStatusBarView(LinearLayout rootView) {
        // 添加等高状态栏:状态栏透明
        if (useStatusBarSpaceView()) {
            mStatusPlaceholderView = new View(getContext());
            mStatusPlaceholderView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtil.getStatuBarHeight(getContext())));
            mStatusPlaceholderView.setBackgroundColor(ContextCompat.getColor(getContext(), getStatusBarColor()));
            rootView.addView(mStatusPlaceholderView);
            // 状态栏顶上去
            StatusBarUtils.transparencyBar(getActivity());
            rootView.setFitsSystemWindows(false);
        } else {
            //  不添加等高状态栏
            if (statusBarTranslate()) {
                // 状态栏透明，body被顶上去，与状态栏重叠
                // 状态栏顶上去
                StatusBarUtils.transparencyBar(getActivity());
                rootView.setFitsSystemWindows(false);
            } else {
                // 状态栏不透明，使用默认的不透明状态栏
                // 状态栏不顶上去
                StatusBarUtils.setStatusBarColor(getActivity(), getStatusBarColor());
                rootView.setFitsSystemWindows(true);
            }
        }
    }

    /**
     * 初始化bodyView
     *
     * @param rootView
     */
    private void initBodyView(LinearLayout rootView) {
        // 初始化body区域
        FrameLayout frameLayout = new FrameLayout(getActivity());
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initBodyLayout(frameLayout);
        initLoadingView(frameLayout);
        rootView.addView(frameLayout);
    }

    /**
     * 初始化主要布局:作为body的最重要部分，使用户的主要界面
     */
    private void initBodyLayout(FrameLayout bodyContainer) {
        // 内容区域
        View bodyView = mLayoutInflater.inflate(getBodyLayoutId(), null);
        bodyView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        bodyContainer.addView(bodyView);
    }

    /**
     * 初始化loadingview
     *
     * @param bodyContainer
     */
    private void initLoadingView(FrameLayout bodyContainer) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void loadError(MessageInfo messageInfo) {

    }

    @Override
    public void showMessage(MessageInfo messageInfo) {

    }
}
